package com.njxr.web.config;

import cn.hutool.core.collection.CollUtil;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;


@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class Swagger2Config {

    @Value("${swagger.enable}")
    private Boolean SWAGGER_ENABLE;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                // 去掉swagger默认的状态码
                .useDefaultResponseMessages(false)
                // 是否开启swagger
                .enable(SWAGGER_ENABLE)
                .apiInfo(apiInfo()).groupName("EvMS-Service-doc").select()
                // 只扫描添加了注解的类
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex("^(?!login).*$")).build()
                // 配置security
//                .securitySchemes(securitySchemes()).securityContexts(securityContexts())
                ;
    }

    @SuppressWarnings("deprecation")
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("EvMS-Service API").description("客户评价系统接口在线文档")
                .contact("wangyue").termsOfServiceUrl("http://127.0.0.1:2000").version("1.0.0")
                .build();
    }


    @Bean
    public UiConfiguration uiConfiguration() {
        return UiConfigurationBuilder.builder()
                .supportedSubmitMethods(new String[]{})
                .build();
    }


    private List<ApiKey> securitySchemes() {
        return CollUtil.newArrayList(new ApiKey("BearerToken", "Authorization", "header"));
    }

    private List<SecurityContext> securityContexts() {
        return CollUtil.newArrayList(SecurityContext.builder().securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("^(?!auth).*$")).build());
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return CollUtil.newArrayList(new SecurityReference("Authorization", authorizationScopes));
    }

}
